package com.hejing.cloud.controller;

import com.hejing.cloud.entities.PayDTO;
import com.hejing.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * ClassName: OrderController
 * Package: com.hejing.cloud.controller
 * Description:
 *
 * @Author: HeJing
 * @Create: 2024-08-03 13:47
 * @Version: 1.0
 */
@RestController
public class OrderController {

    // public static final String PaymentSrv_URL = "http://localhost:8001";  // 硬编码，写死。要避免
    public static final String PaymentSrv_URL = "http://cloud-payment-service";  // 服务注册中心上的微服务名称

    @Resource
    private RestTemplate restTemplate;

    // 增加订单
    @PostMapping(value = "/consumer/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {

        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
    }

    // 根据id删除订单
    // 参考网上的代码
    @GetMapping("/consumer/pay/del/{id}")
    public ResultData deleteOrder(@PathVariable("id") Integer id){
        ResponseEntity<ResultData> response = restTemplate.exchange(PaymentSrv_URL + "/pay/del/" + id, HttpMethod.DELETE, null, ResultData.class, id);
        ResultData result = response.getBody();
        return result;
    }

    // 修改订单
    // 参考网上的代码
    @GetMapping("/consumer/pay/update")
    public ResultData updateOrder(@RequestBody PayDTO payDTO){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<PayDTO> requestEntity = new HttpEntity<>(payDTO, headers);
        ResponseEntity<ResultData > response = restTemplate.exchange(PaymentSrv_URL + "/pay/update", HttpMethod.PUT, requestEntity, ResultData.class, payDTO);
        ResultData result = response.getBody();
        return result;
    }

    // 查询根据id订单
    @GetMapping(value = "/consumer/pay/get/{id}")
    public ResultData<Integer> getPayInfo(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id, ResultData.class, id);
    }


    @GetMapping(value = "/consumer/pay/get/info")
    private String getInfoByConsul()
    {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }


    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/consumer/discovery")
    public String discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        System.out.println("===================================");

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());
        }

        return instances.get(0).getServiceId()+":"+instances.get(0).getPort();
    }
}






































